<template>
  <div >
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/' }">考试公告管理</el-breadcrumb-item>
      <el-breadcrumb-item>考试公告列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-divider></el-divider>

    <el-table :data="tableData" border style="width: 100%">
      <el-table-column prop="id" label="ID" width="60" align="center"></el-table-column>
      <el-table-column prop="title" label="标题" width="200" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="content" label="内容" header-align="center" show-overflow-tooltip></el-table-column>
      <el-table-column prop="type" label="类型" width="100" align="center"></el-table-column>
      <el-table-column prop="status" label="状态" width="100" align="center"></el-table-column>
      <el-table-column prop="examId" label="考试名称" width="100" align="center"></el-table-column>
      <el-table-column prop="thumbUp" label="点赞数" width="100" align="center"></el-table-column>
      <el-table-column label="操作" width="100" align="center">
<!--scope封装表单信息-->
        <template slot-scope="scope">
          <el-button type="primary" icon="el-icon-edit" circle size="mini"
                     @click="showEditFormDialog(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle size="mini"
                     @click="openDeleteConfirm(scope.row)"></el-button>
        </template>
      </el-table-column>
    </el-table>
<!--修改时弹出的框-->
    <el-dialog title="修改公告信息" :visible.sync="dialogFormVisible">
      <el-form :model="editForm">
        <el-form-item label="标题">
          <el-input v-model="editForm.title" placeholder="请输入公告标题"></el-input>
        </el-form-item>
        <el-form-item label="内容">
          <el-input v-model="editForm.content"
                    type="textarea"
                    :autosize="{ minRows: 8, maxRows: 10}"
                    placeholder="请输入内容"></el-input>
        </el-form-item>
        <el-form-item label="类型">
          <el-select v-model="editForm.type" placeholder="请选择活动区域">
            <el-option label="报名时间公告" value="1"></el-option>
            <el-option label="打印准考证公告" value="2"></el-option>
            <el-option label="成绩查询公告" value="3"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="submitEditForm">确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
export default {
  data() {
    return {
      tableData: [],
      dialogTableVisible: false,
      dialogFormVisible: false,
      editForm: {
        title: '',
        content: '',
        type: '',
      },
      formLabelWidth: '80px'
    }
  },
  methods: {
    showEditFormDialog(info) {
      let url = 'http://120.77.154.166:8080/info/' + info.id+'/select';
      console.log('showEditFormDialog:url = ' + url);
      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.editForm = jsonResult.data;
          this.dialogFormVisible = true;
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadInfoList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },
    submitEditForm() {
      let url = 'http://120.77.154.166:8080/info/' + this.editForm.id + '/update';
      console.log('url = ' + url);

      let formData = this.qs.stringify(this.editForm);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url,formData).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '修改公告成功！',
            type: 'success'
          });
          this.dialogFormVisible = false;
          this.loadInfoList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
              this.loadInfoList();
            }
          });
        } else if (jsonResult.state == 40900) {
          this.$message.error(jsonResult.message);
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.dialogFormVisible = false;
            }
          });
        }
      });
    },

    openDeleteConfirm(info) {
      let message = '此操作将永久删除【' + info.title + '】公告，是否继续？';
      this.$confirm(message, '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.handleDelete(info);
      }).catch(() => {
      });
    },
    //向后端发送删除请求
    handleDelete(info) {
      let url = 'http://120.77.154.166:8080/info/' + info.id + '/delete';
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .post(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.$message({
            message: '删除【' + info.title + '】公告成功！',
            type: 'success'
          });
          this.loadInfoList();
        } else if (jsonResult.state == 40400) {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {
              this.loadInfoList();
            }
          });
        } else {
          this.$alert(jsonResult.message, '操作失败', {
            confirmButtonText: '确定',
            callback: action => {}
          });
        }
      });
    },

    loadInfoList() {
      let url = "http://120.77.154.166:8080/info/list";
      console.log('url = ' + url);

      this.axios
          .create({'headers': {'Authorization': localStorage.getItem('jwt')}})
          .get(url).then((response) => {
        let jsonResult = response.data;
        if (jsonResult.state == 20000) {
          this.tableData = jsonResult.data;
        }
      });
    }
  },
  mounted() {
    this.loadInfoList();
  }
}
</script>
